package com.xhrd.framework.web;

import java.io.Serializable;

import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;

import com.xhrd.framework.dao.domain.BaseEntity;
import com.xhrd.framework.utils.GenericsUtils;
import com.xhrd.framework.utils.StringUtil;
import com.xhrd.framework.vo.Message;
import com.xhrd.framework.vo.Messages;
import com.xhrd.framework.vo.Page;
import com.xhrd.framework.vo.PresentationVo;
import com.xhrd.framework.vo.SearchCondition;

/**
 * 业务实体类Controller
 *
 * @author 曹保利
 *
 * @param <Entity>
 * @param <SC>
 */
@SuppressWarnings(value = { "unchecked" })
public class EntityController<Entity extends BaseEntity, SC extends SearchCondition> extends AbstractEntityController<Entity, SC> {

    protected Class<Entity> entityClass = null;

    public EntityController() {
        super();
    }

    @Override
    protected void doCreate(ModelAndView mav) {
        //Entity entity = org.springframework.beans.BeanUtils.instantiate(entityClass);
        entityClass = GenericsUtils.getGenericClass(getClass());
        Entity entity = null;
        try {
            entity = entityClass.newInstance();
            mav.addObject("voModel", entity);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    protected void doEdit(ModelAndView mav, String id) {
        PresentationVo presentationVo = new PresentationVo();
        Entity entity = null;
        Object resultObject = entityService.get(id);
        if (resultObject == null) {
            logger.warn("can't find record by id=" + id);
            entity = (Entity) GenericsUtils.getClassInstance(entityClass);
            Messages messages = new Messages();
            messages.add(new Message("要编辑的数据记录不存在."));
            presentationVo.setErrorMessages(messages);
            presentationVo.setSuccess(false);
        } else {
            presentationVo.setSuccess(true);
            entity = (Entity) resultObject;
        }
        presentationVo.setVoModel(entity);
        mav.addObject("presentationResult", presentationVo);
        mav.addObject("voModel", entity);
    }

    @Override
    protected void doList(ModelAndView mav, SearchCondition searchCondition) {
        prepareSearchCondition(searchCondition);
        Page<Entity> page = null;
        if (doListEnable) {
            page = (Page<Entity>) entityService.searchPage(searchCondition);
            mav.addObject("pageObject", page);
        }
    }

    protected void prepareSearchCondition(SearchCondition searchCondition) {
        // DO NOTHING!
    }

    @Override
    protected void doRemove(ModelAndView mav, Entity entity) {
        entityService.remove(entity);
    }

    @Override
    protected void doRemove(ModelAndView mav, String id, Entity entity) {
        if (StringUtil.isValidString(id)) {
            entityService.remove(id);
        } else {
            entityService.remove((Serializable[]) entity.getIds());
        }
    }

    @Override
    protected void doSave(ModelAndView mav, Entity entity, BindingResult bindingResult) {
        entityService.save(entity);
    }

    protected void doShow(ModelAndView mav, String id) {
        PresentationVo presentationVo = new PresentationVo();
        Entity entity = null;
        Object resultObject = entityService.get(id);
        if (resultObject == null) {
            logger.warn("can't find record by id=" + id);
            entity = (Entity) GenericsUtils.getClassInstance(entityClass);
            Messages messages = new Messages();
            messages.add(new Message("要查看的数据记录不存在."));
            presentationVo.setErrorMessages(messages);
            presentationVo.setSuccess(false);
        } else {
            presentationVo.setSuccess(true);
            entity = (Entity) resultObject;
        }
        decorateShowResult(entity);
        presentationVo.setVoModel(entity);
        mav.addObject("presentationResult", presentationVo);
        mav.addObject("voModel", entity);
    }

    protected void decorateShowResult(Entity entity) {
        // DO NOTHING!
    }

    @Override
    protected void prepareAddData(ModelAndView mav) {

    }

    @Override
    protected void prepareEditData(ModelAndView mav, String id) {

    }

    @Override
    protected void prepareListData(ModelAndView mav) {

    }

}
